//
//  WSLineView.m
//  3333
//
//  Created by iMac on 16/11/10.
//  Copyright © 2016年 zws. All rights reserved.
//

#import "WSLineChartView.h"
#import "YAxisView.h"
#import "XAxisView.h"
#import "XAsyncAxisView.h"
#import "SVProgressHUD.h"
#import "DevChartModel.h"


#define leftMargin 30
#define rightMargin 30
#define topMargin 25   // 为顶部留出的空白
//#define defaultSpace 5
#define lastSpace 0
#define sideSpace 15



@interface WSLineChartView ()

@property (strong, nonatomic) NSArray *xTitleArray;
@property (strong, nonatomic) NSArray *yValueArray;
@property (strong, nonatomic) NSDictionary *yValueDic;
@property (assign, nonatomic) CGFloat yMax;
@property (assign, nonatomic) CGFloat yMin;
@property (strong, nonatomic) NSString *xTypeName;
@property (strong, nonatomic) NSString *yTypeName;
@property (strong, nonatomic) NSString *unit;

@property (strong, nonatomic) YAxisView *leftYAxisView;
@property (strong, nonatomic) YAxisView *rightYAxisView;

@property (strong, nonatomic) XAxisView *xAxisView;
//@property (strong, nonatomic) XAsyncAxisView *xAxisView;
@property (strong, nonatomic) UIScrollView *scrollView;
@property (assign, nonatomic) CGFloat pointGap;
@property (assign, nonatomic) CGFloat defaultSpace;//X轴点之间的间隔

@property (assign, nonatomic) CGFloat moveDistance;

@end

@implementation WSLineChartView


- (instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.backgroundColor = [UIColor clearColor];
        
        self.xTitleArray = @[];
        self.yValueArray = @[];
        self.yMax = .0;
        self.yMin = .0;
        self.xTypeName = @"";
        self.yTypeName = @"";
        self.unit = @"";

        if (self.xTitleArray.count > 600) {
            _defaultSpace = 5;
        }
        else if (self.xTitleArray.count > 400 && self.xTitleArray.count <= 600){
            _defaultSpace = 10;
        }
        else if (self.xTitleArray.count > 200 && self.xTitleArray.count <= 400){
            _defaultSpace = 20;
        }
        else if (self.xTitleArray.count > 100 && self.xTitleArray.count <= 200){
            _defaultSpace = 30;
        }
        else if (self.xTitleArray.count > 10 && self.xTitleArray.count <= 100){
            _defaultSpace = 40;
        }
        else {
            _defaultSpace = (wsScreenWidth-leftMargin-rightMargin-sideSpace*2)/(self.xTitleArray.count-1);
        }


        self.pointGap = (wsScreenWidth-leftMargin-rightMargin-sideSpace*2)/(self.xTitleArray.count-1);
    
        [self creatXAxisView];
        [self creatYAxisView];
        
        // 2. 捏合手势
        UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGesture:)];
        [self.xAxisView addGestureRecognizer:pinch];
        
        //长按手势
        UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(event_longPressAction:)];
        [self.xAxisView addGestureRecognizer:longPress];
        
        //单击手势
        UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapGesture:)];
        singleTap.numberOfTapsRequired = 1;
        [self addGestureRecognizer:singleTap];
    }
    
    return self;
}


- (id)initWithFrame:(CGRect)frame xTitleArray:(NSArray*)xTitleArray yValueArray:(NSArray*)yValueArray yMax:(CGFloat)yMax yMin:(CGFloat)yMin yTypeName:(NSString*)yTypeName xTypeName:(NSString*)xTypeName  unit:(NSString*)unit {

    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        
        self.xTitleArray = xTitleArray;
        self.yValueArray = yValueArray;
        self.yMax = yMax;
        self.yMin = yMin;
        self.xTypeName = xTypeName;
        self.yTypeName = yTypeName;
        self.unit = unit;

        if (xTitleArray.count > 600) {
            _defaultSpace = 5;
        }
        else if (xTitleArray.count > 400 && xTitleArray.count <= 600){
            _defaultSpace = 10;
        }
        else if (xTitleArray.count > 200 && xTitleArray.count <= 400){
            _defaultSpace = 20;
        }
        else if (xTitleArray.count > 100 && xTitleArray.count <= 200){
            _defaultSpace = 30;
        }
        else if (xTitleArray.count > 10 && xTitleArray.count <= 100){
            _defaultSpace = 40;
        }
        else {
            _defaultSpace = (wsScreenWidth-leftMargin-rightMargin-sideSpace*2)/(xTitleArray.count-1);
        }


        self.pointGap = (wsScreenWidth-leftMargin-rightMargin-sideSpace*2)/(xTitleArray.count-1);
        

        [self creatXAxisView];
        [self creatYAxisView];
        
        
        // 2. 捏合手势
        UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGesture:)];
        [self.xAxisView addGestureRecognizer:pinch];
        
        //长按手势
        UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(event_longPressAction:)];
        [self.xAxisView addGestureRecognizer:longPress];
    }
    
    return self;
}


- (id)initWithFrame:(CGRect)frame xTitleArray:(NSArray*)xTitleArray yValueDic:(NSDictionary*)yValueDic yMax:(CGFloat)yMax yMin:(CGFloat)yMin yTypeName:(NSString*)yTypeName xTypeName:(NSString*)xTypeName  unit:(NSString*)unit{
    
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        
        self.xTitleArray = xTitleArray;
        self.yValueDic = yValueDic;
        self.yMax = yMax;
        self.yMin = yMin;
        self.xTypeName = xTypeName;
        self.yTypeName = yTypeName;
        self.unit = unit;

        if (xTitleArray.count > 600) {
            _defaultSpace = 5;
        }
        else if (xTitleArray.count > 400 && xTitleArray.count <= 600){
            _defaultSpace = 10;
        }
        else if (xTitleArray.count > 200 && xTitleArray.count <= 400){
            _defaultSpace = 20;
        }
        else if (xTitleArray.count > 100 && xTitleArray.count <= 200){
            _defaultSpace = 30;
        }
        else if (xTitleArray.count > 10 && xTitleArray.count <= 100){
            _defaultSpace = 40;
        }
        else {
            _defaultSpace = (wsScreenWidth-leftMargin-rightMargin-sideSpace*2)/(xTitleArray.count-1);
        }


        self.pointGap = (wsScreenWidth-leftMargin-rightMargin-sideSpace*2)/(xTitleArray.count-1);
        
        [self creatXAxisView];
        [self creatYAxisView];
    
        // 2. 捏合手势
//        UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGesture:)];
//        [self.xAxisView addGestureRecognizer:pinch];

        //长按手势
//        UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(event_longPressAction:)];
//        [self.xAxisView addGestureRecognizer:longPress];
    }
    
    return self;
}


- (void)setChartModel:(DevChartModel *)chartModel{
    _chartModel = chartModel;
    [_leftYAxisView setChartModel:_chartModel];
    [_rightYAxisView setChartModel:_chartModel];
    [_xAxisView setChartModel:_chartModel];
    [_xAxisView setIsLongPress:NO];
    self.xTitleArray = _chartModel.xTitlesArray;
    if (self.xTitleArray.count > 600) {
        _defaultSpace = 5;
    }
    else if (self.xTitleArray.count > 400 && self.xTitleArray.count <= 600){
        _defaultSpace = 10;
    }
    else if (self.xTitleArray.count > 200 && self.xTitleArray.count <= 400){
        _defaultSpace = 20;
    }
    else if (self.xTitleArray.count > 100 && self.xTitleArray.count <= 200){
        _defaultSpace = 30;
    }
    else if (self.xTitleArray.count > 10 && self.xTitleArray.count <= 100){
        _defaultSpace = 40;
    }
    else {
        _defaultSpace = (wsScreenWidth-leftMargin-rightMargin-sideSpace*2)/(self.xTitleArray.count-1);
    }
    self.pointGap = (wsScreenWidth-leftMargin-rightMargin-sideSpace*2)/(self.xTitleArray.count-1);
}


- (void)creatYAxisView{
    _leftYAxisView = [[YAxisView alloc] initWithFrame:CGRectMake(0, 0, leftMargin+sideSpace, self.frame.size.height)];
    [_leftYAxisView setType:0];
    [self addSubview:_leftYAxisView];
    
    _rightYAxisView = [[YAxisView alloc] initWithFrame:CGRectMake((self.frame.size.width-rightMargin-sideSpace), 0, leftMargin+sideSpace, self.frame.size.height)];
    [_rightYAxisView setType:1];
    [self addSubview:_rightYAxisView];
}

- (void)creatXAxisView {
    
    _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(leftMargin, 0, self.frame.size.width-leftMargin-rightMargin, self.frame.size.height)];
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.bounces = NO;
    _scrollView.scrollsToTop = NO;
    [self addSubview:_scrollView];
    
//    self.xAxisView = [[XAxisView alloc] initWithFrame:CGRectMake(0, 0, (self.xTitleArray.count-1) * self.pointGap + lastSpace, self.frame.size.height) xTitleArray:self.xTitleArray yValueArray:self.yValueArray yMax:self.yMax yMin:self.yMin yTypeName:self.yTypeName xTypeName:self.xTypeName unit:self.unit];
    _xAxisView = [[XAxisView alloc] initWithFrame:CGRectMake(0, 0, _scrollView.size.width+lastSpace,_scrollView.frame.size.height)];
    //_xAxisView = [[XAsyncAxisView alloc] initWithFrame:CGRectMake(0, 0, _scrollView.size.width+lastSpace,_scrollView.frame.size.height)];
    [_scrollView addSubview:_xAxisView];
    _scrollView.contentSize = _xAxisView.frame.size;
    
    NSLog(@"----->xAxisView frame:%@",NSStringFromCGRect(_xAxisView.frame));
}
- (void)layoutSubviews
{
    NSLog(@"----->xAxisView frame:%@",NSStringFromCGRect(_xAxisView.frame));
}

// 捏合手势监听方法
- (void)pinchGesture:(UIPinchGestureRecognizer *)recognizer
{
    if (!_chartModel.isAllowScale) {
        return;
    }
    
    if (recognizer.state == 3) {
        
        if (self.xAxisView.frame.size.width-lastSpace <= self.scrollView.frame.size.width) { //当缩小到小于屏幕宽时，松开回复屏幕宽度
            
            //CGFloat scale = self.scrollView.frame.size.width / (self.xAxisView.frame.size.width-lastSpace);
    
            self.pointGap = (wsScreenWidth-leftMargin-rightMargin-sideSpace*2)/(self.xTitleArray.count-1);
            
            [UIView animateWithDuration:0.25 animations:^{
                
                CGRect frame = self.xAxisView.frame;
                frame.size.width = self.scrollView.frame.size.width+lastSpace;
                self.xAxisView.frame = frame;
                NSLog(@"----->xAxisView222 frame:%@",NSStringFromCGRect(_xAxisView.frame));
            }];
            
            self.xAxisView.pointGap = self.pointGap;
            
        }else if (self.xAxisView.frame.size.width-lastSpace >= ((self.xTitleArray.count-1) * _defaultSpace)+sideSpace*2){
            
            [UIView animateWithDuration:0.25 animations:^{
                CGRect frame = self.xAxisView.frame;
                frame.size.width = (self.xTitleArray.count-1) * self->_defaultSpace + lastSpace+sideSpace*2;
                self.xAxisView.frame = frame;
                NSLog(@"----->xAxisView333 frame:%@",NSStringFromCGRect(_xAxisView.frame));
            }];
            
            self.pointGap = _defaultSpace;
            
            self.xAxisView.pointGap = self.pointGap;
        }
    }else{
        
        CGFloat currentIndex,leftMagin;
        if( recognizer.numberOfTouches == 2 ) {
            //2.获取捏合中心点 -> 捏合中心点距离scrollviewcontent左侧的距离
            CGPoint p1 = [recognizer locationOfTouch:0 inView:self.xAxisView];
            CGPoint p2 = [recognizer locationOfTouch:1 inView:self.xAxisView];
            CGFloat centerX = (p1.x+p2.x)/2;
            leftMagin = centerX - self.scrollView.contentOffset.x;
            
            currentIndex = centerX / self.pointGap;
            
            
            self.pointGap *= recognizer.scale;
            self.pointGap = self.pointGap > _defaultSpace ? _defaultSpace : self.pointGap;
            if (self.pointGap == _defaultSpace) {
                
                [SVProgressHUD showErrorWithStatus:@"Has been put to the maximum"];
            }
            
            self.xAxisView.pointGap = self.pointGap;
            recognizer.scale = 1.0;
            
            if((self.xTitleArray.count-1) * self.pointGap+sideSpace*2 <= self.scrollView.frame.size.width){
                self.pointGap = (wsScreenWidth-leftMargin-rightMargin-sideSpace*2)/(self.xTitleArray.count-1);
            }
            
            self.xAxisView.frame = CGRectMake(0, 0, (self.xTitleArray.count-1) * self.pointGap + lastSpace+sideSpace*2, self.frame.size.height);
            
            self.scrollView.contentOffset = CGPointMake(currentIndex*self.pointGap-leftMagin, 0);
//            NSLog(@"contentOffset = %f",self.scrollView.contentOffset.x);
            NSLog(@"----->xAxisView111 frame:%@",NSStringFromCGRect(_xAxisView.frame));
        }
        
    }
    
    self.scrollView.contentSize = CGSizeMake(self.xAxisView.frame.size.width, 0);
}


- (void)event_longPressAction:(UILongPressGestureRecognizer *)longPress {
    
    if(UIGestureRecognizerStateChanged == longPress.state || UIGestureRecognizerStateBegan == longPress.state) {
        
        CGPoint location = [longPress locationInView:self.xAxisView];
        
        //相对于屏幕的位置
        CGPoint screenLoc = CGPointMake(location.x - self.scrollView.contentOffset.x, location.y);
        [self.xAxisView setScreenLoc:screenLoc];
        
        if (ABS(location.x - _moveDistance) > self.pointGap) { //不能长按移动一点点就重新绘图  要让定位的点改变了再重新绘图
            
            [self.xAxisView setIsShowLabel:YES];
            [self.xAxisView setIsLongPress:YES];
            self.xAxisView.currentLoc = location;
            _moveDistance = location.x;
        }
    }
    
    if(longPress.state == UIGestureRecognizerStateEnded)
    {
        _moveDistance = 0;
        //恢复scrollView的滑动
        [self.xAxisView setIsLongPress:NO];
        [self.xAxisView setIsShowLabel:NO];
    }
}

- (void)singleTapGesture:(UITapGestureRecognizer *)singleTap{
    CGPoint location = [singleTap locationInView:self];
    CGPoint loc = [self convertPoint:location toView:self.xAxisView];
    loc = CGPointMake(MAX(sideSpace,MIN(loc.x, self.xAxisView.size.width+1)), loc.y);
    self.xAxisView.currentLoc = loc;
    if (self.xAxisView.isLongPress) {
        if (CGRectContainsPoint(self.xAxisView.shadowRect,self.xAxisView.currentLoc)) {
            [self.xAxisView setIsLongPress:NO];
            return;
        }
    }
    [self.xAxisView setIsLongPress:YES];
}

@end

